package com.sinosoft.orderinfo;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinosoft.base.GenericManagerImpl;

@Service
public class OrderInfoManagerImp extends GenericManagerImpl<OrderInfo, Long>
		implements OrderInfoManager {
	OrderInfoDao oidao;
	
	@Autowired
	public OrderInfoManagerImp(OrderInfoDao oidao)
	{
		super(oidao);
		this.oidao = oidao;
	}
	
	public OrderInfo saveOrderInfo(OrderInfo o)
	{
		o.setFinishtime(new Date());
		o.setOrderstate(new BigDecimal("1")); 
		return oidao.saveOrderInfo(o);
	}
	
	public OrderInfo getOrderInfoById(String orderid)
	{
		return oidao.getOrderInfoById(orderid);
	}

	public List<OrderInfo> searchOrderInfoListByOrder(OrderInfo orderinfo) {
		String satelliteid = orderinfo.getSatelliteid();
		String username = orderinfo.getUsername();
		String sensorid = orderinfo.getSensorid();
		BigDecimal areadystate = orderinfo.getAreadystate();
		BigDecimal orderstate = orderinfo.getOrderstate();
		BigDecimal sceneid = orderinfo.getSceneid();
		String productlevel = orderinfo.getProductlevel();
		if(productlevel!=null&&!productlevel.equals(""))
			productlevel = productlevel.toUpperCase();
		BigDecimal orderid = orderinfo.getOrderid();
		BigDecimal productid = orderinfo.getProductid();
		String orderstarttime = orderinfo.getOrderstarttime();
		String orderendtime = orderinfo.getOrderendtime();
		String checkstarttime = orderinfo.getCheckstarttime();
		String checkendtime = orderinfo.getCheckendtime();
		StringBuffer hql = new StringBuffer("from OrderInfo o where 1=1");
		hql.append(username!=null&&!username.isEmpty()?" and username like '%"+username.trim()+"%'":"");
		hql.append(sensorid!=null&&!sensorid.isEmpty()?" and sensorid like '%"+sensorid.trim()+"%'":"");
		hql.append(areadystate!=null&&!areadystate.equals("")?" and areadystate = "+areadystate:"");
		hql.append(orderstate!=null&&!orderstate.equals("")?" and orderstate = "+orderstate:"");
		hql.append(satelliteid != null && !satelliteid.isEmpty()?" and satelliteid='%"+satelliteid+"%'":"");
		hql.append(sceneid != null && !sceneid.equals("")?" and sensorid="+sensorid+"":"");
		hql.append(productlevel != null && !productlevel.isEmpty()?" and productlevel='"+productlevel+"'":"");
		hql.append(orderid != null && !orderid.equals("")?" and orderid="+orderid:"");
		hql.append(productid != null && !productid.equals("")?" and productid="+productid:"");
		hql.append(orderstarttime != null && !orderstarttime.isEmpty()?" and tasktime> to_date('"+orderstarttime+"','yyyy-MM-dd HH24:mi:ss')":"");
		hql.append(orderendtime != null && !orderendtime.isEmpty()?" and tasktime<to_date('"+orderendtime+"','yyyy-MM-dd HH24:mi:ss')":"");
		hql.append(checkstarttime != null && !checkstarttime.isEmpty()?" and checktime>to_date('"+checkstarttime+"','yyyy-MM-dd HH24:mi:ss')":"");
		hql.append(checkendtime != null && !checkendtime.isEmpty()?" and checktime<to_date('"+checkendtime+"','yyyy-MM-dd HH24:mi:ss')":"");
		return oidao.searchOrderInfoListByOrder(hql.toString());
	}
	
	public int getOrderInfoCount(OrderInfo orderinfo)
	{
		String satelliteid = orderinfo.getSatelliteid();
		String username = orderinfo.getUsername();
		String sensorid = orderinfo.getSensorid();
		BigDecimal areadystate = orderinfo.getAreadystate();
		BigDecimal orderstate = orderinfo.getOrderstate();
		BigDecimal sceneid = orderinfo.getSceneid();
		String productlevel = orderinfo.getProductlevel();
		if(productlevel!=null&&!productlevel.equals(""))
			productlevel = productlevel.toUpperCase();
		BigDecimal orderid = orderinfo.getOrderid();
		BigDecimal productid = orderinfo.getProductid();
		String orderstarttime = orderinfo.getOrderstarttime();
		String orderendtime = orderinfo.getOrderendtime();
		String checkstarttime = orderinfo.getCheckstarttime();
		String checkendtime = orderinfo.getCheckendtime();
		StringBuffer hql = new StringBuffer("select count(*) from zywx_order_Info o where 1=1");
		hql.append(username!=null&&!username.isEmpty()?" and username like '%"+username.trim()+"%'":"");
		hql.append(sensorid!=null&&!sensorid.isEmpty()?" and sensorid like '%"+sensorid.trim()+"%'":"");
		hql.append(areadystate!=null&&!areadystate.equals("")?" and areadystate = "+areadystate:"");
		hql.append(orderstate!=null&&!orderstate.equals("")?" and orderstate = "+orderstate:"");
		hql.append(satelliteid != null && !satelliteid.isEmpty()?" and satelliteid like '%"+satelliteid+"%'":"");
		hql.append(sceneid != null && !sceneid.equals("")?" and sceneid="+sceneid+"":"");
		hql.append(productlevel != null && !productlevel.isEmpty()?" and productlevel='"+productlevel+"'":"");
		hql.append(orderid != null && !orderid.equals("")?" and orderid="+orderid:"");
		hql.append(productid != null && !productid.equals("")?" and productid="+productid:"");
		hql.append(orderstarttime != null && !orderstarttime.isEmpty()?" and tasktime> to_date('"+orderstarttime+"','yyyy-MM-dd HH24:mi:ss')":"");
		hql.append(orderendtime != null && !orderendtime.isEmpty()?" and tasktime<to_date('"+orderendtime+"','yyyy-MM-dd HH24:mi:ss')":"");
		hql.append(checkstarttime != null && !checkstarttime.isEmpty()?" and checktime>to_date('"+checkstarttime+"','yyyy-MM-dd HH24:mi:ss')":"");
		hql.append(checkendtime != null && !checkendtime.isEmpty()?" and checktime<to_date('"+checkendtime+"','yyyy-MM-dd HH24:mi:ss')":"");
		return oidao.getOrderInfoCount(hql.toString());
	}
	
	public List<OrderInfo> getOrderInfoList(OrderInfo orderinfo,int pagesize,int page)
	{
		return oidao.getOrderInfoList(orderinfo, pagesize,page);
	}
	
	public List<OrderInfo> getOrderListByIds(String ids)
	{
		String sqlstr = "from OrderInfo where orderid in("+ids+")";
		return oidao.getOrderListByIds(sqlstr);
	}
	
	public List<OrderInfo> getOrderinfoListByUsername(String username,String orderids) {
		return oidao.getOrderinfoListByUsername(username,orderids);
	}
	
	public void removeOrderinfo(Long orderid) {
		
		oidao.removeOrderinfo(new Long(orderid));
	}
}
